// pages/deviceInfo/deviceInfo.js
const app = getApp()
var config = require("../config.js")
var curPileInfo = {}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    scale: 14,
    id: 0,
    mac: 0,
    latitude: 0,
    longitude: 0,
    address: "",
    contact: "",
    phone: "",
    code: "",
    newPile: false,
    markers: [
      // {
      //   id: 1025441201,
      //   iconPath: '../images/marker.png',
      //   latitude: 31.498637,
      //   longitude: 120.37257,
      //   width: 20,
      //   height: 32
      // },
    ],
    markerAddr: '',
    markerLong: 0,
    markerLati: 0,

    // 选中的充电桩信息
    currentPile: {
    },
    chargehidden: true,
    noticehidden: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getLocation({
      type: "gcj02", // 坐标系类型
      // 获取经纬度成功回调
      success: (res) => { // es6 箭头函数，可以解绑当前作用域的this指向，使得下面的this可以绑定到Page对象
        console.log("longtitude", res)
        this.setData({  // 为data对象里定义的经纬度默认值设置成获取到的真实经纬度，这样就可以在地图上显示我们的真实位置
          longitude: res.longitude,
          latitude: res.latitude
        })
      },
      fail: (res) => {
        wx.showToast({
          title: '获取GPS失败',
          duration: 4000
        });
        wx.navigateTo({
          url: '../myEquipment/myEquipment',
        })
      }
    });


    wx.getStorage({
      key: 'curPileInfo',
      success: function (res) {
        console.log("getStorage pileInfo: ", res)
        curPileInfo = res.data;

        var _marker = [];
        // 如果不是新添加的设备
        if (!curPileInfo.curPileNew) {
          _marker = [
            {
              id: curPileInfo.id,
              iconPath: '../images/marker.png',
              latitude: curPileInfo.latitude,
              longitude: curPileInfo.longitude,
              width: 20,
              height: 32
            }
          ]
        }
        else {
          _marker = [
            {
              id: curPileInfo.id,
              iconPath: '../images/marker.png',
              latitude: that.data.latitude,
              longitude: that.data.longitude,
              width: 20,
              height: 32
            }
          ]
        }

        that.setData({
          markers: _marker,
          newPile: curPileInfo.curPileNew,
          address: curPileInfo.address,
          contact: curPileInfo.contact,
          phone: curPileInfo.phone,
          code: curPileInfo.code,
          latitude: curPileInfo.latitude,
          longitude: curPileInfo.longitude
        })

      },
      fail: function () {
        console.log("get storage info failed!")
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  onMarkerTap: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        var _marker = that.data.markers;
        _marker[0].longitude = res.longitude
        _marker[0].latitude = res.latitude
        that.setData({
          markers: _marker,
          address: res.address,
          latitude: res.latitude,
          longitude: res.longitude
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onClickGPS: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        var _marker = that.data.markers;
        _marker[0].longitude = res.longitude
        _marker[0].latitude = res.latitude
        that.setData({
          markers: _marker,
          address: res.address,
          latitude: res.latitude,
          longitude: res.longitude
        })

        curPileInfo.address = res.address
        curPileInfo.latitude = res.latitude
        curPileInfo.longitude = res.longitude
      },
    })
  },

  onSave: function (e) {
    var that = this;
    var formData = e.detail.value;
    console.log("folmadat: ", formData)
    curPileInfo.longitude = that.data.longitude
    curPileInfo.latitude = that.data.latitude
    curPileInfo.address = formData.address
    curPileInfo.contact = formData.contact
    curPileInfo.phone = formData.phone
    curPileInfo.code = formData.code
    wx.request({
      url: config.url_all + '/api/charge/devicePile/update',
      // data: {
      //   id: curPileInfo.id,
      //   mac: curPileInfo.mac,
      //   longitude: that.data.longitude,
      //   latitude: that.data.latitude,
      //   address: formData.address,
      //   contact: formData.contact,
      //   phone: formData.phone
      // },
      data: curPileInfo,
      header: {
        Rememberme: wx.getStorageSync('token')
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: (res) => {
        console.log("update res: ", res)
        if (res.data.type == "SUCCESS") {
          wx.showModal({
            content: '更新成功!',
            showCancel: false,
            success: function () {
              // wx.navigateTo({
              //   url: '../deviceList/deviceList',
              // })
              wx.navigateBack({
                delta: 1
              })
            }
          })
        }
        else {
          wx.showModal({
            content: '更新失败!',
            showCancel: false,
            success: function () {
              wx.navigateBack({
                delta: 1
              })
            }
          })

          console.log("fail update res: ", res)
        }


      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})